<?php
include_once 'root.php';
Class Order extends Root {
	public function __construct() {

		parent::__construct();

		$this -> load -> helper('util');
	}

	public function index() {
		$this -> data['header'] = "Order";
		$this -> includeView("createorder");
	}

	public function create() {
		$this -> data['header'] = "เลือกลูกค้า";

		$this -> data['canSearch'] = $this -> canUseFunction("customer", "search");
		$this -> data['canCheckIn'] = $this -> canUseFunction("customer", "edit");

		$this -> load -> model("customermodel");
		$q = $this -> getCoreQuery(true);
		$allRow = $this -> customermodel -> getCustomerCount($this -> checkIDSearch("customer", "viewall"), $q);
		$this -> data['dataList'] = $this -> customermodel -> getCustomer($this -> checkIDSearch("customer", "viewall"), $q, 0, 50);

		$this -> includeView("startcreateorder");
	}

	public function tocreate($custID) {
		$this -> session -> set_userdata('order_type', "");
		$action = (isset($_GET['action'])) ? $_GET['action'] : "first";
		$this -> data['action'] = $action;
		$this -> load -> model("customermodel");
		$this -> load -> model("ordermodel");
		$this -> load -> library('genhtml');

		$this -> data['genhtml'] = $this -> genhtml;
		$customerData = $this -> customermodel -> getACustomer($custID, $this -> checkIDSearch("customer", "viewall"));
		$this -> data['dataList'] = $customerData;
		if ($this -> data['dataList'] != false) {

			$this -> data['priceType'] = $this -> ordermodel -> getPriceType();

			$this -> session -> set_userdata("currentcustomer", $custID);
			$this -> data['header'] = "เปิด order ลูกค้า " . $customerData -> NAME;
			$this -> genhtml -> setData($this -> data['dataList']);
		}

		$this -> includeView("createordercustomer");
	}

	public function editOrder($orderID) {

		$this -> load -> model("ordermodel");
		$this -> data['order'] = $this -> ordermodel -> getOrderHead($orderID);
		$this -> load -> library('genhtml');
		$this -> data['genhtml'] = $this -> genhtml;
		$this -> session -> set_userdata("CORDERID", $orderID);
		if ($this -> data['order'] != false) {
			$this -> data['header'] = "เปิด order ลูกค้า " . $this -> data['order'] -> CustName;
			$this -> genhtml -> setData($this -> data['order']);
		}

		$this -> includeView("editorder");
	}

	public function status() {
		$this -> includeView("wait");
	}

	public function returnOrder() {
		$this -> includeView("wait");
	}

	public function seachproduct() {
		$keyword = $_POST['keyword'];
		$vatType = $_POST['vatType'];
		$priceType = $_POST['priceType'];
		$column = "";
		$unit = "";
		if ($priceType == "ZPR0") {
			$column = "CREDIT_" . $vatType . "_PRICE";
			$unit = "CREDIT_" . $vatType . "_UNIT";
		} else if ($priceType == "ZPR1") {
			$column = "VAN_" . $vatType . "_PRICE";
			$unit = "VAN_" . $vatType . "_UNIT";
		}

		if ($keyword == "") {
			echo json_encode(array("flag" => false, "message" => "กรอกข้อมูล"));
			exit();
		}

		$this -> load -> model("productmodel");
		$data = $this -> productmodel -> searchProductPrice($keyword, $column, $unit);
		if (!empty($data)) {
			$dataCart = $this -> in_cart();
			foreach ($data as $in => $val) {
				if (array_key_exists($val -> MATNR, $dataCart)) {
					$currentSelect = $dataCart[$val -> MATNR];
					$val -> qty = $currentSelect['qty'];
					$val -> rowid = $currentSelect['rowid'];
				} else {
					$val -> qty = 0;
					$val -> rowid = 0;
				}

				$pricePer = $val -> $unit;
				$p = explode("/", $pricePer);
				$priceUnit = $p[1];
				$ratio = 1;

				$baseUnit = $val -> BASE_UNIT;

				if ($priceUnit == "CAR") {
					$ratio = $val -> EA_CARTON;
				} else if ($priceUnit == "PAC") {
					$ratio = $val -> EA_PACK;
				} else if ($priceUnit == "CAN") {
					$ratio = 1;
				} else {
					$ratio = 1;
				}
				$val -> DisplayPrice = number_format($val -> $column / $ratio, 2, '.', '');
				$val -> CalPrice = number_format($val -> $column / $ratio, 4, '.', '');
			}
			echo json_encode(array("flag" => true, "message" => $data));

		} else {

			echo json_encode(array("flag" => false, "message" => "ไม่พบข้อมูลสินค้า"));
			exit();
		}
	}

	public function checkdata() {
		echo $this -> cart -> total_items();

	}

	public function addpromotion() {

		$this -> deletePremium();
		$this -> load -> model("ordermodel");
		$this -> load -> model("promotionmodel");
		$orderID = $this -> session -> userdata("CORDERID");
		$order = $this -> ordermodel -> getOrderHead($orderID);
		$this -> data['order'] = $order;

		$this -> data['header'] = "สรุปรายการสั่งซื้อ";
		$custID = $this -> session -> userdata("currentcustomer");
		$this -> data['orderType'] = $this -> ordermodel -> getOrderType();
		$this -> data['orderReason'] = $this -> ordermodel -> getOrderReason();
		$this -> data['itemCategory'] = $this -> ordermodel -> getItemCategory();
		$this -> data['priceType'] = $this -> ordermodel -> getPriceType();

		$this -> load -> model("customermodel");
		$customerData = $this -> customermodel -> getACustomer($custID, "");

		if ($this -> session -> userdata('order_type') == "") {
			foreach ($_POST as $in => $val) {
				$this -> session -> set_userdata($in, $_POST[$in]);
			}
		}
		$priceType = $order->PriceTypeID;
		
		$intPriceType = preg_replace("/[^0-9,.]/", "", $priceType);

		

		$myCart = $this -> cart -> contents();
		$i = 0;
		$dataPromotion = array();
		$dataLevel = array();

		// เก็บว่าได้โปรโมชั้นตัวไหนบ้าง
		$dataPromotionAll = array();
		// เก็บสินค้าที่ได้รับโปรโมช้น
		$productRecivePromotion = array();
		$productRecivePromotionOption = array();
		$productQty = array();
		$productCAR = array();
		$productPAC = array();
		$productEA = array();
		$productDiscount = array();
		foreach ($this->cart->contents() as $items) {
			if ($items['price'] > 0) {
				$data = $this -> promotionmodel -> getPromotionByMatnr($items['id']);
				$option = $this -> cart -> product_options($items['rowid']);

				if ($data != false) {
					$productRecivePromotion[] = $items;
					$productRecivePromotionOption[] = $option;
					foreach ($data as $j => $k) {
						$promotion = $this -> promotionmodel -> getPromotionByPriceType($k -> PROMO_ID, $intPriceType);
						if ($promotion != false && !isset($dataPromotionAll[$k -> PROMO_ID])) {
							
							$dataPromotionAll[$k -> PROMO_ID] = $promotion;

						}

						if (isset($productQty[$k -> PROMO_ID])) {
							$productQty[$k -> PROMO_ID] = $productQty[$k -> PROMO_ID] + $items['qty'];
						} else {
							$productQty[$k -> PROMO_ID] = $items['qty'];
						}
						$allCount = converUnitToData($items, $option);
						$productQty[$k -> PROMO_ID];
						
						$promotionLevel = $this -> promotionmodel -> getPromotionLevel($k -> PROMO_ID);
						$level = $this -> findPromotionLevel($promotionLevel, $items['qty'], $allCount);
						//print_r($level);
						if ($level != false) {
							if (isset($productCAR[$k -> PROMO_ID])) {
								
								$productCAR[$k -> PROMO_ID] = $productCAR[$k -> PROMO_ID] + $level['KAR']['QTY'];
							} else {
								$productCAR[$k -> PROMO_ID] = $level['KAR']['QTY'];
							}
							if (isset($productPAC[$k -> PROMO_ID])) {

								$productPAC[$k -> PROMO_ID] = $productPAC[$k -> PROMO_ID] + $level['PAK']['QTY'];
							} else {
								$productPAC[$k -> PROMO_ID] = $level['PAK']['QTY'];
							}
							if (isset($productEA[$k -> PROMO_ID])) {
								$productEA[$k -> PROMO_ID] = $productEA[$k -> PROMO_ID] + $level['EA']['QTY'];
							} else {
								$productEA[$k -> PROMO_ID] = $level['EA']['QTY'];
							}
							$sum = $level['KAR']['DISCOUNT'] + $level['PAK']['DISCOUNT'] + $level['EA']['DISCOUNT'];
							if (isset($productDiscount[$k -> PROMO_ID])) {
								$productDiscount[$k -> PROMO_ID] = $productDiscount[$k -> PROMO_ID] + $sum;
							} else {
								$productDiscount[$k -> PROMO_ID] = $sum;
							}

						}
					}
				}
			}

			$i++;
			//print_r($items);
		}
		print_r($productQty);
		/*
		 $size = count($productRecivePromotion);
		 for($i=0;$i<$size;$i++){
		 $items = $productRecivePromotion[$i];
		 $option = $productRecivePromotionOption[$i];
		 $allCount = converUnitToData($items, $option);
		 print_r($allCount);
		 }
		 exit();

		 foreach ($dataPromotionAll as $in => $val) {

		 if ($promotionLevel != false) {

		 $dataLevel[$items['id']] = $level;
		 }
		 }

		 exit();

		 // find promotion level

		 if ($promotion != false) {
		 $dataPromotion[$items['id']] = $promotion;
		 }
		 */

		$this -> data['promotionAll'] = $dataPromotionAll;
		$this -> data['productQty'] = $productQty;
		$this -> data['productCAR'] = $productCAR;
		$this -> data['productPAC'] = $productPAC;
		$this -> data['productEA'] = $productEA;
		$this -> data['productDiscount'] = $productDiscount;
		$this -> data['promotion'] = $dataPromotion;
		$this -> data['promoLevel'] = $dataLevel;
		$this -> data['customer'] = $customerData;
		$this -> data['orderType'] = $this -> findValue($this -> data['orderType'], "order_id", "order_name", $this -> session -> userdata('order_type'));
		$this -> data['orderReason'] = $this -> findValue($this -> data['orderReason'], "reason_id", "reason_name", $this -> session -> userdata('order_reason'));
		$this -> data['itemCategory'] = $this -> findValue($this -> data['itemCategory'], "cat_id", "cat_name", $this -> session -> userdata('item_categories'));
		$this -> data['priceType'] = $this -> findValue($this -> data['priceType'], "price_id", "price_type", $order -> PriceTypeID);
		$this -> includeView("addpromotion");
	}

	private function findPromotionLevel($data, $value, $allCount) {

		$this -> session -> set_userdata("KAR", array("QTY" => 0, "DISCOUNT" => 0));
		$this -> session -> set_userdata("PAK", array("QTY" => 0, "DISCOUNT" => 0));
		$this -> session -> set_userdata("EA", array("QTY" => 0, "DISCOUNT" => 0));

		$premiumQty = 0;
		$queryRemain = $value;
		$discount = 0;
		$dataPromo = array();
		foreach ($allCount as $j => $k) {
			$this -> findPromo($data, $j, $k);
			$dataPromo[$j] = $this -> session -> userdata($j);

		}

		$this -> session -> unset_userdata("KAR");
		$this -> session -> unset_userdata("PAK");
		$this -> session -> unset_userdata("EA");
		return $dataPromo;

	}

	private function findPromo($data, $key, $qty) {

		if ($key != "KAR") {
			//return "";
		}
		$queryRemain = $qty;
		$premiumQty = 0;
		$arrayPremium = array();
		foreach ($data as $in => $val) {
			if ($val -> ORDER_UOM == $key) {

				//echo $queryRemain;
				//echo "<br />";
				$num = floor($queryRemain / $val -> ORDER_QTY);
				//echo $num;
				//echo "<br />";
				if ($num > 0) {

					//
					// $str = "<br />AT LEVEL ".$val->PROMO_LEVEL;
					// $str.="<br />AT UNIT ".$key;
					// $str.="<br />AFTER QTY ".$queryRemain;
					// $str.="<br />CALQTY ".$num;
					// $str.="<br />ReduceQty ".($num*$val -> ORDER_QTY);
					// $str.="<br />GET ".($num * $val -> FREE_QTY);

					$numAdd = ($num * $val -> FREE_QTY);
					$premiumQty += $numAdd;
					//$str.="<br />PREMIUM ".$premiumQty;
					$queryRemain -= ($num * $val -> ORDER_QTY);
					//$str.="<br />AfterQty".$queryRemain;
					//$str.="<br />--------------------------";
					//echo $str;

					$array = array();
					//echo "<br /><br />OF ".$val->FREE_UOM;
					$a = $ses = $this -> session -> userdata($val -> FREE_UOM);

					//echo "<br />before";
					//print_r($a);
					//echo "<br />numAdd ". $numAdd;
					$a['QTY'] = $a['QTY'] + $numAdd;
					$a['DISCOUNT'] = $a['DISCOUNT'] + ($val -> DISC_AMT * $num);
					//echo "<br />after";
					//print_r($a);
					//print_r($a);
					$this -> session -> set_userdata($val -> FREE_UOM, $a);

					$array['premiumQty'] = $premiumQty;
					$array['UNIT'] = $val -> FREE_UOM;
					$array['DISCOUNT'] = $val -> DISC_AMT;
					$arrayPremium[] = $array;
					/*
					 if($val->DISC_AMT > 0 ){
					 //$discount+= $val->DISC_AMT;
					 }*/
				}
			};
		}

		//return $arrayPremium;
	}

	public function viewCart() {
		$dataPost = $this -> Post2Array($_POST);
		$this -> data['orderID'] = $dataPost['OrderID'];
		$this -> load -> model("ordermodel");
		$this -> data['header'] = "สรุปรายการสั่งซื้อ";
		$custID = $this -> session -> userdata("currentcustomer");

		$this -> data['orderType'] = $this -> ordermodel -> getOrderType();
		$this -> data['orderReason'] = $this -> ordermodel -> getOrderReason();
		$this -> data['itemCategory'] = $this -> ordermodel -> getItemCategory();
		$this -> data['priceType'] = $this -> ordermodel -> getPriceType();

		$this -> load -> model("customermodel");
		$customerData = $this -> customermodel -> getACustomer($custID, "");

		$this -> data['customer'] = $customerData;
		$this -> data['orderType'] = $this -> findValue($this -> data['orderType'], "order_id", "order_name", $this -> session -> userdata('order_type'));
		$this -> data['orderReason'] = $this -> findValue($this -> data['orderReason'], "reason_id", "reason_name", $this -> session -> userdata('order_reason'));
		$this -> data['itemCategory'] = $this -> findValue($this -> data['itemCategory'], "cat_id", "cat_name", $this -> session -> userdata('item_categories'));
		$this -> data['priceType'] = $this -> findValue($this -> data['priceType'], "price_id", "price_type", $this -> session -> userdata('price_type'));
		$this -> includeView("viewcart");
	}

	public function insertOrder() {
		$this -> load -> model("ordermodel");
		$maxID = $this -> ordermodel -> getOrderNum();
		$data = $this -> Post2Array($_POST);

		$insert['OrderID'] = "OR-" . date("y") . date("m") . str_pad($maxID + 1, 3, "0", STR_PAD_LEFT);
		$insert['OrderStatus'] = "Taking";
		$insert['CustID'] = $data['KUNNR'];
		$insert['CustName'] = $data['NAME'];
		$insert['Address1'] = $data['ADDRESS'];
		$insert['Telephone'] = $data['TELEPHONE'];
		$insert['VatType'] = $data['vatType'];
		$insert['Email'] = $data['EMAIL'];
		$insert['Fax'] = $data['FAX'];
		//$insert['DocTypeID'] = $data['order_type'];

		/*
		 $orderType = $this -> ordermodel -> getOrderTypeID($data['order_type']);
		 $insert['DocTypeName'] = $orderType -> order_name;

		 $insert['OrderReasonID'] = $data['order_reason'];
		 $order_reason = $this -> ordermodel -> getOrderReasonID($data['order_reason']);
		 $insert['OrderReasonName'] = $order_reason -> reason_name;

		 $insert['ItemCategoryID'] = $data['item_categories'];
		 $category = $this -> ordermodel -> getItemCategoryID($data['item_categories']);
		 $insert['ItemCategoryName'] = $category -> cat_name;
		 */
		$insert['PriceTypeID'] = $data['price_type'];
		$priceType = $this -> ordermodel -> getPriceTypeID($data['price_type']);
		$insert['PriceTypeName'] = $priceType -> price_type;

		$insert['NumOfProduct'] = 0;
		$insert['SaleID'] = $this -> getSaleID();
		$insert['SaleName'] = $this -> getSaleName();
		$insert['OrderDate'] = date("Y-m-d");
		$insert['CreateOn'] = date("Y-m-d H:i:s");
		$insert['UpdateOn'] = date("Y-m-d H:i:s");
		$this -> ordermodel -> createOrder($insert);
		redirect(base_url() . "order/editorder/" . $insert['OrderID']);
		exit();
	}

	public function findValue($data, $key, $value, $values) {
		foreach ($data as $in => $val) {
			$val = (array)$val;
			if ($val[$key] == $values) {
				return $val[$value];
				break;
			}
		}
		return "";
	}

	public function updateCart() {
		$this -> load -> model("productmodel");
		$data = $this -> Post2Array($_POST);
		if (!empty($data)) {
			$qty = $data['qty'];
			$rowID = $data['rowid'];
			$pid = $data['pid'];
			$size = count($rowID);

			/*
			 $column = "";
			 $custType = $data['CustType'];
			 $vatType = $data['VatType'];
			 $this -> session -> set_userdata("vattype", $vatType);
			 if ($custType == "1") {
			 if ($vatType == "v") {
			 $column = "CREDIT_VAT_PRICE";
			 } else {
			 $column = "CREDIT_NOVAT_PRICE";
			 }
			 } else {
			 if ($vatType == "v") {
			 $column = "VAN_VAT_PRICE";
			 } else {
			 $column = "VAN_NOVAT_PRICE";
			 }
			 }
			 */

			for ($i = 0; $i < $size; $i++) {

				if ($rowID[$i] == "0") {

					$cartonQty = $data['cQty'][$i];

					$packQty = $data['pQty'][$i];
					$eqQty = $data['eQty'][$i];
					$checkQty = $cartonQty + $packQty + $eqQty;
					if ($checkQty > 0) {

						$product = $this -> productmodel -> getAProduct($pid[$i]);

						if ($product != false) {

							$cartonQty = $data['cQty'][$i] * $product -> EA_CARTON;
							$packQty = $data['pQty'][$i] * $product -> EA_PACK;
							$eqQty = $data['eQty'][$i] * 1;
							$qty = $cartonQty + $packQty + $eqQty;

							$dataInsert = array('id' => $product -> MATNR, 'qty' => $qty, 'price' => $data['price'][$i], 'name' => "name", 'options' => (array)$product);

							$this -> cart -> insert($dataInsert);

						}
					}

				} else {

					$product = $this -> productmodel -> getAProduct($pid[$i]);
					$cartonQty = $data['cQty'][$i] * $product -> EA_CARTON;
					$packQty = $data['pQty'][$i] * $product -> EA_PACK;
					$eqQty = $data['eQty'][$i] * 1;
					$qty = $cartonQty + $packQty + $eqQty;

					$dataInsert = array('rowid' => $rowID[$i], 'qty' => $qty);
					$this -> cart -> update($dataInsert);
				}
			}
			$this -> set_res("true", "บันทึกข้อมูลเรียบร้อยแล้ว");
		}
	}

	public function tosummaryorder() {
		$this -> load -> model("productmodel");
		$data = $this -> Post2Array($_POST);

		$size = count($data['premiumID']);
		$this -> deletePremium();

		$sizeDicount = count($data['discount']);
		$i = 0;

		$ids = array();
		$qty = array();
		foreach ($this->cart->contents() as $item) {
			$ids[] = $item['rowid'];
			$qty[] = $item['qty'];
		}

		for ($i = 0; $i < count($ids); $i++) {
			$discount = $data['discount'][$i];
			$arr = array('rowid' => $ids[$i], 'qty' => $qty[$i], 'discount' => $discount);

			$this -> cart -> update($arr);
		}

		for ($i = 0; $i < $size; $i++) {
			$rowID = $this -> isPremium($data['premiumID'][$i]);

			if ($rowID == "") {

				$product = $this -> productmodel -> getAProduct($data['premiumID'][$i]);

				if ($product != false) {

					$cartonQty = $data['addKAR'][$i] * $product -> EA_CARTON;
					$packQty = $data['addPAK'][$i] * $product -> EA_PACK;
					$eqQty = $data['addEA'][$i] * 1;

					//echo "<br />cartion is".$cartonQty;
					//echo "<br />pack is".$packQty;
					//echo "<br />eqty is".$eqQty;

					$qty = $cartonQty + $packQty + $eqQty;
					$product -> ISPremium = "yes";

					if ($qty > 0) {
						$dataInsert = array('id' => $product -> MATNR, 'qty' => $qty, 'price' => 0, 'name' => "name", 'options' => (array)$product);
						$this -> cart -> insert($dataInsert);

					}

				} else {

				}
			} else {
				$c = $this -> cart -> contents();
				$oldQty = $c[$rowID]['qty'];

				$product = $this -> productmodel -> getAProduct($data['premiumID'][$i]);
				$cartonQty = $data['addKAR'][$i] * $product -> EA_CARTON;
				$packQty = $data['addPAK'][$i] * $product -> EA_PACK;
				$eqQty = $data['addEA'][$i] * 1;
				$qty = $cartonQty + $packQty + $eqQty;

				$dataInsert = array('rowid' => $rowID, 'qty' => $qty + $oldQty);
				$this -> cart -> update($dataInsert);
			}
		}
		redirect(base_url() . "order/viewallorder");
	}

	public function out() {
		$this -> cart -> destroy();
	}

	public function viewallorder() {
		$this -> data['header'] = "สรุปรายการ Order ทั้งหมด";
		$this -> includeView("viewallorder");
	}

	public function createOrder() {
		$allData = array();
		$orderHead = array();
		$orderID = $this -> session -> userdata("CORDERID");
		$i = 1;
		$productSale = 0;
		$productPremium = 0;
		$totalAmount = 0;
		$totalDiscount = 0;

		foreach ($this->cart->contents() AS $item) {
			$option = $this -> cart -> product_options($item['rowid']);
			$data = array();
			$discount = 0;
			$qty = $item['qty'];
			$totalDiscount += $discount;
			$lineAmount = ($item['price'] * $item['qty']);
			$totalAmount += $lineAmount;
			$data['OrderID'] = $orderID;
			$data['pID'] = $item['id'];
			$data['pName'] = $option['MAKTX'];
			$data['price'] = $item['price'];
			$data['qty'] = $item['qty'];
			$data['discount'] = $discount;
			$data['LineAmount'] = $lineAmount;
			$data['SaleUnit'] = $option['BASE_UNIT'];
			$data['PackRatio'] = $option['EA_PACK'];
			$data['CartonRatio'] = $option['EA_CARTON'];
			$data['priceAfterDisc'] = $lineAmount - $discount;
			$docQty = 0;
			if ($option['BASE_UNIT'] == "EA") {
				$docQty = $qty;
			} else if ($option['BASE_UNIT'] == "PAK") {
				$docQty = $qty / $option['EA_PACK'];
			} else {
				$docQty = $qty / $option['EA_CARTON'];
			}
			$data['DocQty'] = $docQty;
			if ($lineAmount == 0) {
				$data['isSaleProduct'] = "TANN";
				$productPremium++;
			} else {
				$data['isSaleProduct'] = "TAN";
				$productSale++;
			}
			$data['seqNo'] = $i;

			$allData[] = $data;
			$i++;
		}
		$orderHead['NumOfProduct'] = count($this -> cart -> contents());
		$orderHead['NumOfProductSale'] = $productSale;
		$orderHead['NumOfProductPremium'] = $productPremium;
		$orderHead['TotalAmount'] = $totalAmount;
		$orderHead['Discount'] = $totalDiscount;
		$orderHead['PriceAfterDisc'] = $totalAmount - $totalDiscount;
		$this -> load -> model("ordermodel");
		$this -> ordermodel -> insertOrder($allData, $orderHead);

		$this -> cart -> destroy();
		redirect(base_url() . "order/");
	}

	public function in_cart($product_id = null) {

		if ($this -> cart -> total_items() > 0) {

			$in_cart = array();
			// Fetch data for all products in cart
			foreach ($this->cart->contents() AS $item) {
				$in_cart[$item['id']] = array("qty" => $item['qty'], "rowid" => $item['rowid']);
			}

			return $in_cart;
		}
		return array(); ;
	}

	public function isPremium($product_id = null) {
		$find = "";
		if ($this -> cart -> total_items() > 0) {

			$in_cart = array();
			// Fetch data for all products in cart
			foreach ($this->cart->contents() AS $item) {
				if ($item['id'] == $product_id && $item['price'] == 0) {
					$find = $item['rowid'];
					break;
				}
			}

		}
		return $find;

	}

	public function deletePremium() {
		$id = array();
		if ($this -> cart -> total_items() > 0) {

			$in_cart = array();
			// Fetch data for all products in cart
			foreach ($this->cart->contents() AS $item) {
				if ($item['price'] == 0) {
					$id[] = $item['rowid'];
				}
			}
		}
		foreach ($id as $i) {
			$data = array('rowid' => $i, 'qty' => 0);
			$this -> cart -> update($data);
		}

	}

	public function addToCart() {

	}

}
?>